var rootUrl = "capi/V2/expirationReminder/memberUser/"
var storageName = "userInfo";
var wx_synchronization_flag = 0;
var netTool
// var checkdata = require('../utils/dataCheek')
var page = {
	pageSize: 10,
	total: 0,
	currentPage: 1
}
/**
 * wx_synchronization_flag = 1表示已经拉取用户信息
 */
function UserInfo(_netTool) {
	netTool = _netTool
	// 检查权限
	this.isVip =  function(fn) {
		let userInfo = uni.getStorageSync('userInfo')
		if(userInfo){
			let tiem = new Date().getTime()
			let endTime = new Date(userInfo.vip_end_time).getTime()
			 
			if(endTime > tiem ){
				typeof fn == 'function' && fn({
					success:true,
					level:userInfo.level
				});
			}else{
				typeof fn == 'function' && fn({
					success:false,
					 
				});
			}
		}
	},
	// 用户登录
	this.login = function(data, fn) {
		console.log(data)
		var that = this;
		  let info = {
			  user_id:data.user_id
		  }
		netTool.getAjaxUrlValue(rootUrl + 'list', { // mark: xxx
				...data
			},  res =>{
				console.log(res)
				console.log('已有账号')
		 
				if(res.records != 0){
					res.login = true
					uni.setStorageSync('userInfo',res.rows[0])
					uni.setStorageSync('isLogin',true)
					typeof fn == 'function' && fn(res);
				}else { 
				 
					netTool.getAjaxUrlValue(rootUrl + 'save', { 
							level:'-1',   
							...data
						},r => {
							console.log('save',r)
							netTool.getAjaxUrlValue(rootUrl + 'findById',{
								id:r.id,
								is_deleted: 0,
							},res => {
								if(res.records == 0){
									uni.$u.toast('系统错误')
									return
								}
								console.log(res)
								if(res.success){
								
									let userInfo = res.rows
									let addTime = res.rows.add_time
									let date = new Date(addTime)
									let year = date.getFullYear()
									let month = date.getMonth() + 1
									let nowStamp = date.getTime()
									let days = new Date(year, month, 0).getDate()
									let next = 1000 * 60 * 60 * 24 * days
									let expirationTime = new Date(nowStamp + next)
									userInfo.level = '0'
									userInfo.user_identity = '0'//0主人，1工作人，如果是店主直接店主如果加入别人的店就是工作人员
									userInfo.vip_end_time = nowStamp + next
									userInfo.vip_start_time = nowStamp
									netTool.getAjaxUrlValue(rootUrl + 'update', {
										...userInfo
									}, R => {
										console.log(R)
									})
									uni.setStorageSync('userInfo',userInfo)
									uni.setStorageSync('isLogin',true)
									console.log(userInfo)
								
									uni.navigateTo({
										url:'./newUser'
									})
								} 
							})
						})
					// netTool.getAjaxUrlValue('capi/V2/ERShopManage/erShopManage/save',{
					// 	shop_name:'基础店铺', 
					// },rback => {
					// 	netTool.getAjaxUrlValue(rootUrl + 'save', {
					// 		password:'12345678',
					// 		level:'-1',
					// 		nick_name :'新用户',
					// 		store_id:rback.id,
					// 		...data
					// 	}, r =>{
					// 		netTool.getAjaxUrlValue('capi/V2/ERShopManage/erShopManage/update',{
								 
					// 			id:rback.id,
					// 			owner_id:r.id

					// 		},R=>{})
							
					// 		data.nick_name ='新用户'
					// 		typeof fn == 'function' && fn(data);
					// 	})
					// })
					// console.log('新用户1')
					
					
				}
			})
	}
	this.list = function(data, fn) {
		netTool.getAjaxUrlValue(rootUrl + 'list', {
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.save = function(data, fn) {
		uni.showLoading({
			title:'正在注册...'
		})
		netTool.getAjaxUrlValue(rootUrl + 'save', {
			...data
		}, function(res) {
			netTool.getAjaxUrlValue(rootUrl + 'findById',{
				id:res.id,
				is_deleted: 0,
			},res => {
				uni.showLoading({
					title:'初始化账号...'
				})
				console.log(res)
				if(res.success){ 
					let userInfo = res.rows
					let addTime = res.rows.add_time
					let date = new Date(addTime)
					let year = date.getFullYear()
					let month = date.getMonth() + 1
					let nowStamp = date.getTime()
					let days = new Date(year, month, 0).getDate()
					let next = 1000 * 60 * 60 * 24 * days
					let expirationTime = new Date(nowStamp + next)
					userInfo.level = '0'
					userInfo.user_identity = '0'//0主人，1工作人，如果是店主直接店主如果加入别人的店就是工作人员
					userInfo.vip_end_time = nowStamp + next
					userInfo.vip_start_time = nowStamp
					netTool.getAjaxUrlValue(rootUrl + 'update', {
						...userInfo
					}, R => {
						if(R.success){
							uni.hideLoading()
						}else{
							uni.hideLoading()
						}
						console.log(R)
					})
					console.log(userInfo)
					uni.hideLoading()
					uni.setStorageSync('isLogin',true)
					uni.setStorageSync('userInfo',res.rows)
					// uni.navigateTo({
					// 	url:'./newUser'
					// })
				} 
			})
			typeof fn == 'function' && fn(res);
		})
	}
	this.getInfo = function(data, fn) {
		netTool.getAjaxUrlValue(rootUrl + 'findById', {
			id: uni.getStorageSync('user_id'),
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.getAreaIds = function( fn) {
		netTool.getAjaxUrlValue(rootUrl + 'getAreaIds', {
			id: uni.getStorageSync('user_id'),
			
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.updateMyself = function(data, fn){
		netTool.getAjaxUrlValue(rootUrl + 'update', {
			id: uni.getStorageSync('user_id'),
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.update = function(data, fn) {
		if (data.real_name == 'undefined') {
			data.real_name = ''
		}
		var that = this;
		netTool.getAjaxUrlValue(rootUrl + 'update', data, function(res) {
			
			let _chache = that.get()
			_chache.id
			if (_chache) {
				let _cache1 = {
					id: _chache.id,
					..._chache,
					...data
				}

				uni.setStorageSync('userInfo', _cache1)
				_chache = that.get()
				typeof fn == "function" && fn(_chache);
			} else {
				netTool.getAjaxUrlValueBackground(rootUrl+'list', {
					id: data.id
				}, function(res) {
					uni.setStorageSync('userInfo', res.rows[0])
					typeof fn == "function" && fn(res.rows[0]);
				})
			}
		})
	}
	this.updateLBS = function(data){
		netTool.getAjaxUrlValueBackground(rootUrl + 'update', data, function(res) {
			typeof fn == "function" && fn(res.rows[0]);
			
		})
	}
	this.getUserProfile = function() {
		// 推荐使用uni.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
		// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
		let _cache = uni.getStorageSync(storageName)
		if (_cache) {
			wx_synchronization_flag = _cache.wx_synchronization_flag
		}
		if (wx_synchronization_flag) {

			return true
		}
		let that = this;
		uni.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				wx_synchronization_flag = 1
				let rawData = JSON.parse(res.rawData)
				that.updateSycn({
					avatar: rawData.avatarUrl,
					wx_synchronization_flag: 1,
					nick_name: rawData.nickName
				})
			}
		})
	}
	this.listDriver = function(data,fn){
		netTool.getAjaxUrlValueBackground('capi/V2/drivers/memberUser/list', {
			...data
		}, function(res) {
			typeof fn == "function" && fn(res);
		})
	}
	this.get = function() {
		return uni.getStorageSync('userInfo')
	}
	this.getMyId = function() {
		return uni.getStorageSync('userInfo').id
	}
	this.getOpenId = function() {
		return uni.getStorageSync('userInfo').open_id
	}
}
module.exports = UserInfo;
